﻿using System;
using System.Collections.Generic;
using System.Text;

namespace StarBotDotNet.TerranExample1
{
    public class TerranExample1Agent : IAgent
    {
        int sleepTime = int.Parse(System.Configuration.ConfigurationSettings.AppSettings["sleepTime"]);
        UnitManager units = new UnitManager();

        public TerranExample1Agent()
        {

        }

        #region IAgent Members

        public virtual void start()
        {
            ProxyBot bot = ProxyBot.Proxy;

            CurrentPlayer.PlayerID = bot.PlayerID;

            //units.AddParty(new MiningParty(bot));
            //units.AddParty(new BuildingParty(bot));
            //units.AddParty(new MiningParty(bot));
            //units.AddParty(new BuildingParty(bot));
            //units.AddParty(new MiningParty(bot));
            //units.AddParty(new BuildingParty(bot));

            units.AddParty(new MiningParty(bot,0));
            BuildingParty bp = new BuildingParty(bot,1);
            units.AddParty(bp);
            //units.AddParty(new MiningParty(bot,2));
            //units.AddParty(new BuildingParty(bot,3));
            //units.AddParty(new MiningParty(bot,4));
            //units.AddParty(new BuildingParty(bot,5));


            int currentTime = 0;

            while (true)
            {
                // This try/catch is useless.
                try
                {
                    System.Threading.Thread.Sleep(sleepTime);
                }
                catch (Exception)
                {
                }

                currentTime += sleepTime;
                units.RunUnitManager(bot.Units, currentTime, bot);

                foreach (UnitParty up in units.Parties.ToArray())
                {
                    if (up.GetHashCode() == 1) // building
                    {
                        if (((BuildingParty)up).IsBuildingTargets == false)
                        {
                            units.Parties.Remove(bp);
                            bp = new BuildingParty(bot, 0);
                            units.AddParty(bp);
                        }
                    }
                }
            }
        }

        #endregion
    }
}
